﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace ddb_BT1
{
    class DataProviderAdapter
    {
        /*
         * Phải sử dụng hàm getdatatable mới sử dụng được hàm insert and update
         * */
        public SqlDataAdapter Adapter;
        public DataTable Table;
        public DataTable getDataTable(string SQL)
        {
            try
            {
                Table = new DataTable();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = DataProviderConnection.getConnectSqlServer();
                cmd.CommandText = SQL;
                cmd.CommandType = CommandType.Text;
             
                Adapter = new SqlDataAdapter(cmd);
                Adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                Adapter.Fill(Table);
                return Table;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return null;
        }
        public void Insert_Update()
        {
            try
            {
                SqlCommandBuilder cb = new SqlCommandBuilder(Adapter);
                Adapter.Update(Table);
                Table.AcceptChanges();
            }
            catch(Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
    
}
